package co.edu.unal.franquicias.mastercard.representations;

import java.util.Date;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import co.edu.unal.franquicias.mastercard.domain.CreditCard;
import co.edu.unal.franquicias.mastercard.domain.CreditCardTransaction;
import co.edu.unal.franquicias.mastercard.domain.ValidationStatus;
import co.edu.unal.franquicias.mastercard.exceptions.InvalidTransactionException;

@XmlRootElement(name = "transaction")
public class CreditCardTransactionRepresentation extends Representation {

	@XmlElement(name = "card")
	private CreditCard card;
	@XmlElement(name = "amount")
	private Float amount;
	@XmlElement(name = "date")
	private Date date;
	@XmlElement(name = "validationStatus")
	private ValidationStatus validationStatus;
	
	CreditCardTransactionRepresentation() {
	}
	
	public static CreditCardTransactionRepresentation createResponseTransactionRepresentation(CreditCardTransaction transaction, VisaUri visaUri) {
            return new CreditCardTransactionRepresentation(transaction, 
                    new Link(Representation.SELF_REL_VALUE, visaUri));
    }
	
	public CreditCardTransactionRepresentation(CreditCardTransaction transaction, Link... links) {
        try {
        	this.amount = transaction.getAmount();
        	this.card = transaction.getCard();
        	this.date = transaction.getDate();
        	this.validationStatus = transaction.getValidationStatus();
            this.links = java.util.Arrays.asList(links);
        } catch (Exception ex) {
            throw new InvalidTransactionException(ex);
        }
    }

    public CreditCardTransaction getTransaction() {
    	return new CreditCardTransaction(card, amount, date, validationStatus);
    }
	
}
